<?php
class TestAnswerLinkage extends CActiveRecord {
    const LINKTYPE_QUESTION = 'questionLinkType';
    const LINKTYPE_CONCLUSION = 'conLinkType';
	
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public function tableName() {
		return strtolower('PsyTestAnswerLinkage');
	}
	
	public function relations() {
        return array(
            'answer' => array(self::BELONGS_TO, 'TestAnswers', 'answerId',
                'joinType' => 'RIGHT JOIN'
            ),
            'question' => array(self::BELONGS_TO, 'TestQuestions', 'linkToId',
            ),
            'conclusion' => array(self::BELONGS_TO, 'TestConclusions', 'linkToId',
            )
        );
	}

	public function rules() {
		return array(
            array('answerId, linkToId, linkType', 'required'),
            array('linkToId, answerId', 'numerical')
		);
	}

    public function attributeLabels() {
        return array(
            'linkToType' => '跳转目标类型',
            'linkToId' => '跳转目标',
        );
    }

    static public function linkType2Text() {
        return array(
            self::LINKTYPE_QUESTION => '题目',
            self::LINKTYPE_CONCLUSION => '结论'
        );
    }

    static public function getLinkTypes() {
        return array(self::LINKTYPE_QUESTION, self::LINKTYPE_CONCLUSION);
    }

    public function getLinkTo() {
        if($this->linkToType == self::LINKTYPE_QUESTION) {
            return $this->question;
        } else if($this->linkToType ==  self::LINKTYPE_CONCLUSION) {
            return $this->conclusion;
        }
    }

    public function getTypeText() {
    	$linkType2Text = self::linkType2Text();
    	return isset($linkType2Text[$this->linkType]) ? $linkType2Text[$this->linkType] : '未知状态 #'.$this->linkType;
    }
}
